<template>
  <div>
      <el-row>
  <el-button round >购物车</el-button>
</el-row>
<el-form label-position="top" label-width="80px" :model="user">
  <el-form-item label="用户名">
     <el-input style="width:300px" v-model="user.username"></el-input>
  </el-form-item>
  <el-form-item label="密码">
    <el-input style="width:300px" v-model="user.password"></el-input>
  </el-form-item>
   <el-form-item label="邮箱">
    <el-input style="width:300px" v-model="user.email"></el-input>
  </el-form-item>
   <el-form-item label="工作">
    <el-input style="width:300px" v-model="user.job"></el-input>
  </el-form-item>
   <el-form-item label="余额">
    <el-input style="width:300px" v-model="user.wallet"></el-input>
  </el-form-item>
   <el-form-item label="头像">
     <el-image
      style="width: 100px; height: 100px"
      :src="user.pic"
      fit="fit"></el-image>
  </el-form-item>
</el-form>
        <el-button size="big"  @click="findOne">修改</el-button>



<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
    <el-form label-position="left" label-width="80px" :model="user">
  <el-form-item label="用户名">
     <el-input  v-model="user.username"></el-input>
  </el-form-item>
  <el-form-item label="密码">
    <el-input  v-model="user.password"></el-input>
  </el-form-item>
   <el-form-item label="邮箱">
    <el-input v-model="user.email"></el-input>
  </el-form-item>
   <el-form-item label="工作">
    <el-input v-model="user.job"></el-input>
  </el-form-item>
   <el-form-item label="余额">
    <el-input v-model="user.wallet"></el-input>
  </el-form-item>
  <el-form-item label="头像">
    <el-upload
  class="avatar-uploader"
  action="http://localhost:8088/user/uploadPic"
  :show-file-list="false"
  :with-credentials="true"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
    <el-image
      style="width: 100px; height: 100px"
      :src="user.pic"
      fit="fit"></el-image>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editById">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  name:'',
  data () {
    return {
        user:{},
         dialogVisible: false,
         imageUrl:''
      }
  },
  created () {

  },
  mounted () {
      this.findUser();
  },
  methods: {
    handleAvatarSuccess(res, file) {
        this.user.pic=res
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
    handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
       tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
      },
      editById(){
        this.dialogVisible=false;
         Vue.axios.post('http://localhost:8088/user/updateUser',this.user).then(res=>{
           if(res.data.code==0){
             const h = this.$createElement;
                this.$message({
                message: h('p', null, [
                    h('span', null, ''),
                    h('i', { style: 'color: teal' }, res.data.message)
                ])
                });
                this.findAll();
           }
           const h = this.$createElement;
                this.$message({
                message: h('p', null, [
                    h('span', null, ''),
                    h('i', { style: 'color: teal' }, res.data.message)
                ])
                });
         })
      },
      findOne(){
          this.dialogVisible=true;
      },

      findUser(){
          Vue.axios.post("http://localhost:8088/user/findUser").then(res=>{
              if(res.data.code==0){
                  var x=JSON.parse(res.data.data)
                  this.user=x;
              }else{
                  alert("ss")
                  const h = this.$createElement;
                this.$message({
                message: h('p', null, [
                    h('span', null, ''),
                    h('i', { style: 'color: teal' }, res.data.message)
                ])
                });
              }
          })
      }
  }
}
</script>
<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

